import 'package:dio/dio.dart';
import 'api_client_manager.dart';
import 'api_error_processor.dart';
import 'response_data.dart';

class ApiClient {
  /// get method
  static Future<ResponseData> get(
      String path, {
        Map<String, dynamic>? queryParameters, // Dio参数
        Options? options, // Dio参数
        CancelToken? cancelToken, // Dio参数
        ProgressCallback? onReceiveProgress, // Dio参数
        bool isThrowError = false, // 用于控制是否由自己处理异常
      }) async {
    Response response;
    try {
      response = await ApiClientManager.instance.dio().get(path,
          queryParameters: queryParameters,
          options: options,
          cancelToken: cancelToken,
          onReceiveProgress: onReceiveProgress);
    } on DioException catch (e) {
      if (isThrowError) {
        rethrow;
      }
      return ApiErrorProcessor.processError(e);
    }
    return response.data;
  }

  /// post method
  static Future<ResponseData> post(
      String path, {
        Object? data, // Dio参数
        Map<String, dynamic>? queryParameters, // Dio参数
        Options? options, // Dio参数
        CancelToken? cancelToken, // Dio参数
        ProgressCallback? onSendProgress, // Dio参数
        ProgressCallback? onReceiveProgress, // Dio参数
        bool isThrowError = false, // 用于控制是否由自己处理异常
      }) async {
    Response response;
    // if(ApiClientManager.postPaths.contains(path)){
    //   return ResponseData(true, -11, '', null, path: path);
    // }
    try {
      response = await ApiClientManager.instance.dio().post(path,
          data: data,
          queryParameters: queryParameters,
          options: options,
          cancelToken: cancelToken,
          onSendProgress: onSendProgress,
          onReceiveProgress: onReceiveProgress);
    } on DioException catch (e) {
      print('DioException======== $e');
      // ApiClientManager.postPaths.remove(path);
      if (isThrowError) {
        rethrow;
      }
      return ApiErrorProcessor.processError(e);
    }
    return response.data;
  }

  /// put method
  static Future<ResponseData> put(
      String path, {
        data, // Dio参数
        Map<String, dynamic>? queryParameters, // Dio参数
        Options? options, // Dio参数
        CancelToken? cancelToken, // Dio参数
        ProgressCallback? onSendProgress, // Dio参数
        ProgressCallback? onReceiveProgress, // Dio参数
        bool isThrowError = false, // 用于控制是否由自己处理异常
      }) async {
    Response response;
    try {
      // if(ApiClientManager.putPaths.contains(path)){
      //   return ResponseData(true, -11, '', null, path: path);
      // }
      response = await ApiClientManager.instance.dio().put(path,
          data: data,
          queryParameters: queryParameters,
          options: options,
          cancelToken: cancelToken,
          onSendProgress: onSendProgress,
          onReceiveProgress: onReceiveProgress);
    } on DioException catch (e) {
      // ApiClientManager.putPaths.remove(path);
      if (isThrowError) {
        rethrow;
      }
      return ApiErrorProcessor.processError(e);
    }
    return response.data;
  }

  /// delete method
  static Future<ResponseData> delete(
      String path, {
//    data, // Dio参数
        Map<String, dynamic>? queryParameters, // Dio参数
        Options? options, // Dio参数
        CancelToken? cancelToken, // Dio参数
        bool isThrowError = false, // 用于控制是否由自己处理异常
      }) async {
    Response response;
    try {
      // if(ApiClientManager.deletePaths.contains(path)){
      //   return ResponseData(true, -11, '', null, path: path);
      // }
      response = await ApiClientManager.instance.dio().delete(path,
          queryParameters: queryParameters,
          options: options,
          cancelToken: cancelToken);
    } on DioError catch (e) {
      // ApiClientManager.deletePaths.remove(path);
      if (isThrowError) {
        throw e;
      }
      return ApiErrorProcessor.processError(e);
    }
    return response.data;
  }

  /// download file
  static Future<ResponseData> download(
      String urlPath,
      savePath, {
        ProgressCallback? onReceiveProgress, // Dio参数
        Map<String, dynamic>? queryParameters, // Dio参数
        CancelToken? cancelToken, // Dio参数
        data, // Dio参数
        Options? options, // Dio参数
        bool isThrowError = false, // 用于控制是否由自己处理异常
      }) async {
    if (options == null) {
      // 默认数据接收超时时间为：60秒
      options = new Options();
      options.receiveTimeout = const Duration(seconds: 60);
    }
    Response response;
    try {
      response = await Dio().download(
          urlPath, savePath,
          onReceiveProgress: onReceiveProgress,
          queryParameters: queryParameters,
          cancelToken: cancelToken,
          data: data,
          options: options);
    } on DioError catch (e) {
      if (isThrowError) {
        throw e;
      }
      return ApiErrorProcessor.processError(e);
    }

    if (response.data is ResponseBody) {
      ResponseBody responseBody = response.data;
      if (responseBody.statusCode == 200) {
        return new ResponseData(true, 0, "下载完成", responseBody.extra, path: urlPath);
      } else {
        return new ResponseData(true, -1, "下载失败", responseBody.extra, path: urlPath);
      }
    }
    return response.data;
  }
}